define(['Framework7'], function (Framework7) {

    var pageCtrl = {
        loginPage: function () {
            var localUserId =localStorage.getItem("user_id");
            var localToken = localStorage.getItem('user_token');
            var loginTime = localStorage.getItem('login_time');
            if(localUserId&&localToken&&loginTime){//token登录
                pageCtrl.loginWithToken(localUserId,localToken,loginTime);
            }else{
                mainView.router.loadPage('login.html');
                yunSpaceApp.showTab('#mainView');
                yunSpaceApp.params.swipePanel = false;
            }
        },
        orderInfo:function(query){
            yunSpaceApp.showIndicator();
            var url =appMain.apiRoot+'vendor/order_info';
            $$.ajax({
                url:url,
                type:'get',
                dataType:'json',
                data:{
                    'order_id':query.id
                },
                success: function (data) {
                    if(data){
                        var html = templates.showOrderInfoTemplate({shows:data});
                        $$('#orderInfo').html(html);
                        yunSpaceApp.hideIndicator();
                    }else{
                        yunSpaceApp.alert('获取信息失败！！','提示');
                        yunSpaceApp.hideIndicator();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })
        },
        homePageData:function(userId){
            var url = appMain.apiRoot+'vendor/user_order_list';
            yunSpaceApp.params.swipePanel = 'left';
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':userId
                },
                success:function(data){
                    var html = templates.showOrderTemplate({shows:data});

                    yunSpaceApp.closeModal('.popup-login');
                    mainView.router.loadPage('order.html');
                    setTimeout(function(){
                        yunSpaceApp.hideIndicator();
                        $$('#orderList').html(html);
                        var ptrContent = $$('#order .pull-to-refresh-content');
                        ptrContent.on('refresh',function(){
                            pageCtrl.orderListRefresh(userId);
                        });
                        $('#moreOrderList').on('click', function () {
                            pageCtrl.loadMoreOrderList();
                        })
                    },100)
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })
        },

        loginWithToken:function(localUserId,localToken,loginTime){
            yunSpaceApp.showIndicator();
            var code= '';
            var url = appMain.apiRoot+ 'vendor/login_with_token';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':localUserId,
                    'token':localToken,
                    'login_time':loginTime
                },
                success:function(data){
                    code = data.code;
                    if(code == 1){//
                        $('.myName').text(data.username);
                        $('.myAccount span').text(data.fullname);
                        pageCtrl.homePageData(localUserId);
                    }
                    if(code == 2||code == 3){
                        yunSpaceApp.popup('.popup-login');
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    code = 4;
                    yunSpaceApp.hideIndicator();
                }
            });

        },
        orderListRefresh:function(userId){
            var url =appMain.apiRoot+ 'vendor/user_order_list';
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':userId
                },
                success:function(data){
                    var html = templates.showOrderTemplate({shows:data});
                    yunSpaceApp.closeModal('.popup-login');
                    mainView.router.loadPage('order.html');
                    setTimeout(function(){
                        yunSpaceApp.hideIndicator();
                        $$('#orderList').html(html);
                    },100)
                    yunSpaceApp.pullToRefreshDone();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        logOut:function(){
            localStorage.removeItem('login_time');
            localStorage.removeItem('user_id');
            localStorage.removeItem('user_token');
            yunSpaceApp.closePanel();
            pageCtrl.loginPage();

            //yunSpaceApp.showTab('#mainView');
        },
        personInfo:function(){
            yunSpaceApp.showIndicator();
            yunSpaceApp.closePanel();
            yunSpaceApp.showTab('#mainView');
            var localUserId =localStorage.getItem("user_id");
            var url =appMain.apiRoot+ 'vendor/user_info';
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':localUserId
                },
                success:function(data){
                    var html = templates.showPersonInfoTemplate({balance:data.balance,orders:data.orders});
                    $('#person').html(html);
                    console.log(data);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })

        },
        loadMoreOrderList : function(){
            yunSpaceApp.showIndicator();
            var localUserId =localStorage.getItem("user_id");
            var last_id = $('#orderList a:last').data('id');
            var url = appMain.apiRoot+ 'vendor/load_more_order_list';
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':localUserId,
                    'last_id':last_id
                },
                success:function(data){
                    var html = templates.showOrderTemplate({shows:data});
                    $('#orderList').append(html);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })
        },
        serviceList : function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_service_list';
            var localUserId =localStorage.getItem("user_id");
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':localUserId
                },
                success:function(data){
                    var html = templates.showServiceTemplate({shows:data});
                    $('#serviceList').html(html);
                    var ptrContent = $$('#service .pull-to-refresh-content');
                    ptrContent.on('refresh',function(){
                        setTimeout(function(){
                            yunSpaceApp.pullToRefreshDone()
                        },200)
                    });
                    $('#moreServiceList').on('click');
                    yunSpaceApp.hideIndicator();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })
        },
        serviceInfo: function (query) {
            var url = appMain.apiRoot+ 'vendor/get_service_info';
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'service_id':query.id
                },
                success:function(data){
                    var html = templates.showServiceInfoTemplate({shows:data});
                    $('#serviceInfo').html(html);
                    var mySwiper = new Swiper('.swiper-container', {
                        //preloadImages: false,
                        //lazyLoading: true,
                        //loop:true,
                        autoplay:3000,
                        effect:'slide',
                        fade: {
                            crossFade: true
                        },
                        pagination: '.swiper-pagination'
                    })
                    console.log(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })
        },
        moreServiceList : function(){

        },
        getMessages : function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_messages';
            var localUserId =localStorage.getItem("user_id");
            $$.ajax({
                type:'get',
                dataType:'json',
                url:url,
                cache:false,
                data:{
                    'user_id':localUserId
                },
                success:function(data){
                    var html = templates.showMessageTemplate({shows:data});
                    $('#messageList').html(html);
                    console.log(data);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(textStatus);
                    console.log(errorThrown);
                    yunSpaceApp.hideIndicator();
                }
            })
        }
    }

    return pageCtrl
})